package billrestaurant.extra.upload;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import org.springframework.web.multipart.MultipartFile;

public class FilesUpload {
	 private MultipartFile[] file;  
	  
	 
	  
	 
	 public MultipartFile[] getFile() {
		return file;
	}
	public void setFile(MultipartFile[] file) {
		this.file = file;
	}
	public String[] moves(String destPaths[] ) throws Exception{
		 return moves(destPaths,null);
	 }
	 public String[] moves(String destPaths ) throws Exception{
		 return movesToSameLocation( destPaths);
	 }
	 public String[] moves(String destPaths[],String names[]) throws Exception{
		 if(null==file )throw new Exception("Moving uploadedFiles Exception["+this.getClass()+"] no files uploaded!!");
			
		 if(null==destPaths )throw new Exception("Moving uploadedFiles Exception["+this.getClass()+"]Destinations paths must be a non null array of string representing the path of each file uploaded");
		 if(destPaths.length!=file.length )throw new Exception("Moving uploadedFiles Exception["+this.getClass()+"] the number of Destinations paths must be a egual to the number of  files uploaded .[apparently size of paths is "+destPaths.length+" and this of files uploaded is "+file.length+"]");
		 if(null!=names&&names.length!=destPaths.length )throw new Exception("Moving uploadedFiles Exception["+this.getClass()+"] the number of Destinations paths must be a egual to the number of  files name given or must be set to null  .[apparently size of paths is "+destPaths.length+" and this of names is "+names.length+"]");
		String res[]=new String[file.length];	
		 if(null==names){
			 int j=0;
				for (MultipartFile f : this.file) {
					res[j++]=move(destPaths[j], null, f);
				}
			}else{
				 int j=0;
					for (MultipartFile f : this.file) {
						res[j++]=move(destPaths[j], names[j], f);
					}
			}
		  return res;
	 }
	 public String[] movesToSameLocation(String destPaths ) throws Exception{
		 return movesToSameLocation(destPaths,null);
	 }
			
		 
	 public String[] movesToSameLocation(String destPaths,String names[]) throws Exception{
		 if(null==file )throw new Exception("Moving uploadedFiles Exception["+this.getClass()+"] no files uploaded!!");
			
		 if(null==destPaths )throw new Exception("Moving uploadedFiles Exception["+this.getClass()+"]Destinations paths must be a non null array of string representing the path of each file uploaded");
		  if(null!=names&&names.length!=file.length )throw new Exception("Moving uploadedFiles Exception["+this.getClass()+"] the number of Destinations paths must be a egual to the number of  files name given or must be set to null  .[apparently size of uploaded files is "+file.length+" and this of names is "+names.length+"]");
		String res[]=new String[file.length];	
		 if(null==names){
			 int j=0;
				for (MultipartFile f : this.file) {
					res[j++]=move(destPaths, null, f);
				}
			}else{
				 int j=0;
					for (MultipartFile f : this.file) {
						res[j++]=move(destPaths, names[j], f);
					}
			}
		  return res;
	 }
	 protected String move(String destPath,String name,MultipartFile file) throws Exception{
		 if(null==file )throw new Exception("Moving uploadedFile Exception["+this.getClass()+"] uploaded file is null  at "+MultipartFile.class.toString());
		 if(null==destPath )throw new Exception("Moving uploadedFile Exception["+this.getClass()+"]Destination path must not be null");
					
		  if(null==name)
		  name=genname(file.getOriginalFilename());
		  
		  try {  
		   InputStream inputStream = file.getInputStream();  
		  if(!destPath.trim().endsWith(File.separator))
			  destPath+=File.separator;
		  if(!new File(destPath).exists())
			  new File(destPath).mkdir();
		   File newFile = new File(destPath + name);  
		   if (!newFile.exists()) {  
		    newFile.createNewFile();  
		   }  
		   FileOutputStream outputStream = new FileOutputStream(newFile);  
		   int read = 0;  
		   byte[] bytes = new byte[1024];  
		  
		   while ((read = inputStream.read(bytes)) != -1) {  
		    outputStream.write(bytes, 0, read);  
		    System.out.println("copying "+read+" byte to "+newFile.getAbsolutePath());
		   }  
		   return newFile.getName();
		  } catch (IOException e) {  
		   // TODO Auto-generated catch block  
		   e.printStackTrace();  
		  }  
		  return null;
	 }

	private String genname(String originalFilename) {
		// TODO Auto-generated method stub
		if(null==originalFilename||originalFilename.lastIndexOf(".")<0) return (long)(Math.random()*100000000+5000000)+"";
	 String ext=originalFilename.substring(originalFilename.lastIndexOf(".")+1);
		  return (long)(Math.random()*100000000+5000000)+"."+ext;
	}
}
